﻿#include"unrealircd.h"
ModuleHeader MOD_HEADER
= {
	"third/messageModify",
	"1.1",
	"This module modify user related information",
	"waitch",
	"unrealircd-5",
};

CMD_FUNC(cmd_emailAdd);
CMD_FUNC(cmd_emailDel);
CMD_FUNC(cmd_emailModify);
CMD_FUNC(cmd_phoneModify);

MOD_TEST(){

	return MOD_SUCCESS;
}

MOD_INIT()
{
    CommandAdd(modinfo->handle, "EMAILADD", cmd_emailAdd, 3, CMD_USER);
    CommandAdd(modinfo->handle, "EMAILDEL", cmd_emailDel, 1, CMD_USER);
	CommandAdd(modinfo->handle, "EMAILMODIFY", cmd_emailModify, 2, CMD_USER);
	CommandAdd(modinfo->handle, "PHONEMODIFY", cmd_phoneModify, 2, CMD_USER);
	return MOD_SUCCESS;
}

MOD_LOAD()
{
	return MOD_SUCCESS;
}

MOD_UNLOAD()
{
	return MOD_SUCCESS;
}


/*
 * 添加新邮箱
 * parv[1]: 新邮箱号
 * parv[2]: 验证码
 * parv[3]: 权限
 */
CMD_FUNC(cmd_emailAdd){
    if (parc != 4 || BadPtr(parv[3])) {
		sendnumeric(client, ERR_NEEDMOREPARAMS, "EMAILADD");
		return;
	}
    if(!is_digist_string(parv[3],strlen(parv[3]))){
        sendnumeric(client, PARAMERROR, "EMAILADD","parv[3]");
        return;
    }
    //判断验证码是否有效
	if(!D_SMSVerificationCode(parv[1],parv[2],SMS_MODE_EMAIL,SMS_STATUS_REGISTER)){
		sendnumeric(client, SMSVALIDATIONFAILED);
		return;
	}
    
    D_im_user_email * user_email= db_createUserEmail();
    user_email->id = safe_alloc(sizeof(int));
    *(user_email->id) = client->user_id;
    safe_strdup(user_email->email,parv[1]);
    user_email->permition = safe_alloc(sizeof(int));
    *(user_email->permition) = atoi(parv[3]);
    
    int status =0;
    char errormessage[200];
    D_im_user_email * temp = insertImUserEmail(user_email,&status,errormessage);
    db_D_UserEmailFree(user_email,1);
    db_D_UserEmailFree(temp,status);
    if(status<0){
        sendnumeric(client, EMAILADDFAILE, "database error");
		return;
    }
    sendnumeric(client, EMAILADDSUCCESS);
};

/*
 * 删除邮箱
 * parv[1]: 邮箱号
 */
CMD_FUNC(cmd_emailDel){
    if (parc != 2 || BadPtr(parv[1])) {
		sendnumeric(client, ERR_NEEDMOREPARAMS, "EMAILDEL");
		return;
	}
    int datalen = 0;
    D_im_user_email * user_email  = queryImUserEmailByEmail(parv[1],&datalen);
    if(datalen != 1){
        sendnumeric(client, PARAMERROR, "EMAILDEL","parv[1]");
        return;
    }
    if(*(user_email->id) != client->user_id){
        db_D_UserEmailFree(user_email,datalen);
        sendnumeric(client, EMAILDELFAILE, "Unable to delete the email that is not yours");
        return;
    }
    int tempLen=0;
    D_im_user_email * temp = deleteImUserEmailByIdAndEmail(client->user_id,user_email->email,&tempLen);
    db_D_UserEmailFree(user_email,datalen);
    if(tempLen != 1){
        sendnumeric(client, EMAILDELFAILE, "Unable to delete the email that is not yours");
        return;
    }
    db_D_UserEmailFree(temp,tempLen);
    sendnumeric(client, EMAILDELSUCCESS);
}

/*
 * 修改邮箱权限
 * parv[1]: 邮箱号
 * parv[2]: 权限
 */
CMD_FUNC(cmd_emailModify){
    if (parc != 3 || BadPtr(parv[2])) {
		sendnumeric(client, ERR_NEEDMOREPARAMS, "EMAILMODIFY");
		return;
	}
    if(!is_digist_string(parv[2],strlen(parv[2]))){
        sendnumeric(client, PARAMERROR, "EMAILADD","parv[2]");
        return;
    }
    int datalen = 0;
    D_im_user_email * user_email  = queryImUserEmailByEmail(parv[1],&datalen);
    if(datalen != 1){
        sendnumeric(client, PARAMERROR, "EMAILADD","parv[1]");
        return;
    }
    int permition = atoi(parv[2]);
    if(*(user_email->id) != client->user_id){
        db_D_UserEmailFree(user_email,datalen);
        sendnumeric(client, EMAILMODIFYFAILE, "Can't modify someone else's email");
        return;
    }
    *(user_email->permition) = permition;
    int status =0;
    char errormessage[200];
    D_im_user_email * temp = updateImUserEmail(user_email,&status,errormessage);
    db_D_UserEmailFree(user_email,datalen);
    db_D_UserEmailFree(temp,status);
    if(status<0){
        sendnumeric(client, EMAILMODIFYFAILE, "database error");
		return;
    }
    sendnumeric(client, EMAILMODIFYSUCCESS);
}



/*
 * 修改手机号
 * parv[1]: 手机号
 * parv[2]: 验证码
 */
CMD_FUNC(cmd_phoneModify){
    if (parc != 3 || BadPtr(parv[2])) {
		sendnumeric(client, ERR_NEEDMOREPARAMS, "PHONEMODIFY");
		return;
	}
    //判断验证码是否有效
	if(!D_SMSVerificationCode(parv[1],parv[2],SMS_MODE_PHONE,SMS_STATUS_REGISTER)){
		sendnumeric(client, SMSVALIDATIONFAILED);
		return;
	}

    int datalen = 0;
    D_User * user = queryImUserById(client->user_id,&datalen);
    if(datalen == -1){
        sendnumeric(client, PHONEMODIFYFAILE, "database error");
        return;
    }
    safe_strdup(user->tel,parv[1]);
    int status = 0;
    char error[200];
    D_User * t_user = updataImUser(user,&status,error);
    db_D_UserFree(user,datalen);
    db_D_UserFree(t_user,status);
    if(status == -1){
        sendnumeric(client, PHONEMODIFYFAILE, "database error");
    }else{
        sendnumeric(client, PHONEMODIFYSUCCESS);
    }
}